//定义关于counter的store
import { defineStore } from "pinia";
import { Service } from "../requests/index.ts";
import { message } from "ant-design-vue";
import project from "./project.js";

/*defineStore 是需要传参数的，其中第一个参数是id，就是一个唯一的值，
简单点说就可以理解成是一个命名空间.
第二个参数就是一个对象，里面有三个模块需要处理，第一个是 state，
第二个是 getters，第三个是 actions。
*/
const useSDH = defineStore("sdh", {
  state: () => ({
    sdhList: [], //SDH列表
    currentSDH: {}, //当前SDH名称
    currentNet: "NETONE", //当前网络
  }),

  getters: {
    //基本使用
    sdhNameListSelect(state) {
      return state.sdhList.map((item) => {
        return { value: item.name, label: item.name };
      });
    },
  },

  actions: {
    //基本使用
    selectCurrentSDH(name) {
      return (this.currentSDH.name = name);
    },
    //获取SDH列表
    async getSDHList(projectName, net = "NETONE") {
      this.currentNet = net;
      const res = await Service.queryLinkUsingGet(projectName, "SDH", net);
      if (res.respCode === 200) {
        console.log("getSDHList", res);
        this.sdhList = res.data;
      } else {
        message.error(res.message);
      }
      return res.data;
    },
    //添加单个SDH
    async addSingleSDH(projectName, sdh, net = null) {
      const currentNetValue = net || this.currentNet;
      console.log("sdh", sdh);
      const res = await Service.uploadSingSDHLinkUsingPost(
        projectName,
        sdh,
        currentNetValue
      );
      // console.log("res",res)
      if (res.respCode === 200) {
        this.sdhList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("addSingleSDH",this.sdhList)
      return this.sdhList;
    },
    //文件上传
    async uploadSDHFile(file, projectName, net = null) {
      console.log("uploadSDHFile", file, projectName, net);
      const currentNetValue = net || this.currentNet;
      const res = await Service.uploadLinkUsingPost(
        file,
        projectName,
        "SDH",
        currentNetValue
      );
      console.log("uploadSDHFile", res);
      if (res.respCode === 200) {
        this.sdhList = res.data;
        message.success(file.name + "上传成功");
        return res.data;
      } else {
        message.error(res.message);
      }
      // console.log("addSingleSDH",this.sdhList)
      return this.sdhList;
    },
    //删除SDH
    async delSingleSDH(projectName, name, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.deleteNetUsingDelete(
        projectName,
        name,
        "SDH",
        currentNetValue
      );
      console.log("deleteSDHUsingDelete", res);
      if (res.respCode === 200) {
        this.sdhList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.sdhList;
    },

    //修改单个SDH
    async updateSingleSDH(projectName, sdh, net = null) {
      const currentNetValue = net || this.currentNet;
      const res = await Service.modifySDHUsingPut(
        projectName,
        sdh,
        currentNetValue
      );
      console.log("updateSingleSDH", res);
      if (res.respCode === 200) {
        this.sdhList = res.data;
        return res.data;
      } else {
        message.error(res.message);
      }
      return this.sdhList;
    },
  },
});

//暴露这个sdh模块
export default useSDH;
